package com.zhika.zishi.internet;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.zhika.zishi.MyApplication;
import com.zhika.zishi.constant.InternetConstant;
import com.zhika.zishi.constant.OthersConstant;
import com.zhika.zishi.util.LogUtil;

import org.apache.http.Header;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import org.json.JSONStringer;

/**
 * Created by shi-02 on 2015/5/5.
 * 网络请求 post
 */
public class PostInternet {

    private static final String TAG = "CommonInternet";


    /**
     * 发送验证码
     */
    public static void sendIdentifyCode(String telephone,Context context, final Handler handler) {


        String url = InternetConstant.SERVER_ADDRESS + InternetConstant.REQUEST_CODE;
        LogUtil.e(TAG, "URL:" + url);

        try {

            //请求参数： {"telephone":"11111111"}
            JSONStringer stringer = new JSONStringer().object()
                    .key("telephone").value(telephone)
                    .endObject();

            StringEntity entity = new StringEntity(stringer.toString(), HTTP.UTF_8);

            LogUtil.e(TAG, "entity:" + stringer.toString());

            MyApplication.asyncHttpClient.post(context, url, entity, InternetConstant.CONTENT_TYPE, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    LogUtil.e(TAG, "返回码：" + i);
                    String jsonData = new String(bytes);
                    LogUtil.e(TAG, "返回的json:" + jsonData);

                    //解析json，并通过handler返回给主线程结果
                    parseSendIdentifyCodeJson(jsonData,handler);
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                    LogUtil.e(TAG, "返回的错误码：" + i);
//                    LogUtil.e(TAG, new String(bytes) + ";" + throwable.getMessage());
                    throwable.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析发送验证码返回的数据
     * @param jsonData
     * @param handler
     */
    private static void parseSendIdentifyCodeJson(String jsonData,Handler handler){
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            boolean isSuccess = jsonObject.getBoolean("success");
            Message message = Message.obtain();
            message.what = OthersConstant.SEND_IDENTIFY_CODE_HANDLER;

            //发送成功
            if (isSuccess){
                message.arg1 = 1;
            }
            //发送失败
            else {
                message.arg1 = 2;
                String errorStr = jsonObject.getString("error");
                message.obj = errorStr;
            }
            handler.sendMessage(message);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
